<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>How it works</title>
<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
<link rel="up" href="../boost_pfr.html" title="Chapter 26. Boost.PFR 2.3">
<link rel="prev" href="pfr_as_a_c_20_module.html" title="PFR as a C++20 module">
<link rel="next" href="acknowledgements.html" title="Acknowledgements">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td>
<td align="center"><a href="../../../index.html">Home</a></td>
<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="pfr_as_a_c_20_module.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../boost_pfr.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="acknowledgements.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="boost_pfr.how_it_works"></a><a class="link" href="how_it_works.html" title="How it works">How it works</a>
</h2></div></div></div>
<h3>
<a name="boost_pfr.how_it_works.h0"></a>
      <span class="phrase"><a name="boost_pfr.how_it_works.fields_count_detection_and_getti"></a></span><a class="link" href="how_it_works.html#boost_pfr.how_it_works.fields_count_detection_and_getti">Fields count
      detection and getting references to members</a>
    </h3>
<p>
      Short description:
    </p>
<div class="orderedlist"><ol class="orderedlist" type="1">
<li class="listitem">
          at compile-time: use aggregate initialization to detect fields count in
          user-provided structure
          <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
                <span class="bold"><strong>BOOST_PFR_USE_CPP17 == 1</strong></span>:
                <div class="orderedlist"><ol class="orderedlist" type="a"><li class="listitem">
                      at compile-time: structured bindings are used to decompose
                      a type <code class="computeroutput"><span class="identifier">T</span></code> to
                      known amount of fields
                    </li></ol></div>
              </li>
<li class="listitem">
                <span class="bold"><strong>BOOST_PFR_USE_CPP17 == 0 &amp;&amp; BOOST_PFR_USE_LOOPHOLE
                == 1</strong></span>:
                <div class="orderedlist"><ol class="orderedlist" type="a">
<li class="listitem">
                      at compile-time: use aggregate initialization to detect fields
                      count in user-provided structure
                    </li>
<li class="listitem">
                      at compile-time: make a structure that is convertible to anything
                      and remember types it has been converted to during aggregate
                      initialization of user-provided structure
                    </li>
<li class="listitem">
                      at compile-time: using knowledge from previous steps create
                      a tuple with exactly the same layout as in user-provided structure
                    </li>
<li class="listitem">
                      at compile-time: find offsets for each field in user-provided
                      structure using the tuple from previous step
                    </li>
<li class="listitem">
                      at run-time: get pointer to each field, knowing the structure
                      address and each field offset
                    </li>
<li class="listitem">
                      at run-time: a tuple of references to fields is returned =&gt;
                      all the tuple methods are available for the structure
                    </li>
</ol></div>
              </li>
<li class="listitem">
                <span class="bold"><strong>BOOST_PFR_USE_CPP17 == 0 &amp;&amp; BOOST_PFR_USE_LOOPHOLE
                == 0</strong></span>:
                <div class="orderedlist"><ol class="orderedlist" type="a">
<li class="listitem">
                      at compile-time: let <code class="computeroutput"><span class="identifier">I</span></code>
                      be is an index of current field, it equals 0
                    </li>
<li class="listitem">
<p class="simpara">
                      at run-time: <code class="computeroutput"><span class="identifier">T</span></code>
                      is constructed and field <code class="computeroutput"><span class="identifier">I</span></code>
                      is aggregate initialized using a separate instance of structure
                      that is convertible to anything
                    </p>
<div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
                        Additional care is taken to make sure that all the information
                        about <code class="computeroutput"><span class="identifier">T</span></code> is
                        available to the compiler and that operations on <code class="computeroutput"><span class="identifier">T</span></code> have no side effects,
                        so the compiler can optimize away the unnecessary temporary
                        objects.
                      </p></td></tr>
</table></div>
</li>
<li class="listitem">
                      at compile-time: <code class="computeroutput"><span class="identifier">I</span>
                      <span class="special">+=</span> <span class="number">1</span></code>
                    </li>
<li class="listitem">
                      at compile-time: if <code class="computeroutput"><span class="identifier">I</span></code>
                      does not equal fields count goto step <em class="replaceable"><code>c.</code></em>
                      from inside of the conversion operator of the structure that
                      is convertible to anything
                    </li>
<li class="listitem">
                      at compile-time: using knowledge from previous steps create
                      a tuple with exactly the same layout as in user-provided structure
                    </li>
<li class="listitem">
                      at compile-time: find offsets for each field in user-provided
                      structure using the tuple from previous step
                    </li>
<li class="listitem">
                      at run-time: get pointer to each field, knowing the structure
                      address and each field offset
                    </li>
</ol></div>
              </li>
</ul></div>
        </li>
<li class="listitem">
          at run-time: a tuple of references to fields is returned =&gt; all the
          tuple methods are available for the structure
        </li>
</ol></div>
<p>
      Long description of some basics: <a href="https://youtu.be/UlNUNxLtBI0" target="_top">Antony
      Polukhin: Better C++14 reflections</a>. Long description of some basics
      of C++14 with <a class="link" href="limitations_and_configuration.html" title="Limitations and Configuration"><span class="bold"><strong>BOOST_PFR_USE_LOOPHOLE == 0</strong></span></a>: <a href="https://youtu.be/abdeAew3gmQ" target="_top">Antony
      Polukhin: C++14 Reflections Without Macros, Markup nor External Tooling</a>.
      Description of the <span class="bold"><strong>BOOST_PFR_USE_LOOPHOLE == 1</strong></span>
      technique by its inventor Alexandr Poltavsky <a href="http://alexpolt.github.io/type-loophole.html" target="_top">in
      his blog</a>.
    </p>
<h3>
<a name="boost_pfr.how_it_works.h1"></a>
      <span class="phrase"><a name="boost_pfr.how_it_works.field_name_retrieval"></a></span><a class="link" href="how_it_works.html#boost_pfr.how_it_works.field_name_retrieval">Field
      name retrieval</a>
    </h3>
<div class="orderedlist"><ol class="orderedlist" type="1">
<li class="listitem">
          at compile-time:
          <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
                Get references to members of an object of type <code class="computeroutput"><span class="identifier">T</span></code>
                in <code class="computeroutput"><span class="keyword">constexpr</span></code> function
              </li>
<li class="listitem">
                Feed the reference from previous as a template parameter to a <code class="computeroutput"><span class="keyword">constexpr</span></code> function with <code class="computeroutput"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">auto</span> <span class="identifier">member_ptr</span><span class="special">&gt;</span></code>. That function returns <code class="computeroutput"><span class="identifier">__PRETTY_FUNCTION__</span></code> or some other
                vendor specific macro that prints the whole name of a function along
                with the template arguments.
              </li>
<li class="listitem">
                The returned value from previous step contains the member name (<a href="https://godbolt.org/z/K4aWdcE9G" target="_top">godbolt example</a>). Do
                some compiler specific parsing of the value and make a <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">string_view</span></code> that contains only
                the member name.
              </li>
</ul></div>
        </li>
<li class="listitem">
          at run-time: return the <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">string_view</span></code>
          with the member name.
        </li>
</ol></div>
</div>
<div class="copyright-footer">Copyright © 2016-2025 Antony Polukhin<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="pfr_as_a_c_20_module.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../boost_pfr.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="acknowledgements.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
